package com.example.cognitivesystemserver.controller;

import com.example.cognitivesystemserver.Exceptions.ParamsException;
import com.example.cognitivesystemserver.Model.UserModel;
import com.example.cognitivesystemserver.base.ResultInfo;
import com.example.cognitivesystemserver.mapper.LoginMapper;
import com.example.cognitivesystemserver.pojo.Login;
import com.example.cognitivesystemserver.pojo.Student;
import com.example.cognitivesystemserver.service.LoginService;
import com.example.cognitivesystemserver.utils.AssertUtil;
import com.example.cognitivesystemserver.utils.Base64Util;
import com.example.cognitivesystemserver.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    //注册
    @PostMapping("/login/register")
    @ResponseBody
    public Object register(String name, String userId,String userPwd,int role){

        log.info("name:{}",name);
        log.info("Id:{}",userId);
        log.info("password:{}",userPwd);

        ResultInfo resultInfo=new ResultInfo();

        try {
            UserModel userModel=loginService.register(name,userId,userPwd,role);
            resultInfo.setResult(userModel);
            resultInfo.setMsg("登录成功");

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
        }

        return resultInfo;

    }

    //登录
    @GetMapping("/login/LoginAcc")
    public ResultInfo Login(String userId, String userPwd){
        ResultInfo resultInfo=new ResultInfo();

        //通过try catch捕获service层的异常
        try{
            //调用Service层登录方法
            UserModel userModel=loginService.login(userId,userPwd);

            //设置ResultInfo的result，登录成功的信息，将数据返回给请求
            resultInfo.setResult(userModel);

        }catch (ParamsException p){
            //失败状态码
            resultInfo.setCode(p.getCode());
            //失败信息
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            //自定义异常
            resultInfo.setCode(500);
            resultInfo.setMsg("登录失败！");
        }
        return resultInfo;
    }

    @PostMapping("/user/updatePwd")
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword,String newPassword,String repeatPassword){
        ResultInfo resultInfo=new ResultInfo();
        try {
            //得到cookie中的学生id
            String userId;
            String userIdString= CookieUtil.getCookieValue(request,"userIdStr");
            if(StringUtils.isBlank(userIdString)){
                userId=null;
            }
            userId= Base64Util.decoderUserID(userIdString);
            loginService.updatePassWord(userId,oldPassword,newPassword,repeatPassword);
        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("修改密码失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    @RequestMapping("/main")
    public String main(HttpServletRequest request){
        String userId;
        //获取cookie中的id
        String userIdString= CookieUtil.getCookieValue(request,"userIdStr");
        if(StringUtils.isBlank(userIdString)){
            userId=null;
        }
        userId= Base64Util.decoderUserID(userIdString);
        //设置session
        Login login=loginService.selectByUserId(userId);
        request.getSession().setAttribute("user",login);

        return "main";

    }
}
